/* GUI CONVERTED from rssupd.p (converter v1.76) Mon Apr 29 23:36:34 2002 */
/* rssupd.p - Routine to generate Shipping Schedules                        */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                      */
/* All rights reserved worldwide.  This is an unpublished work.             */
/* $Revision: 1.27 $                                                         */
/*V8:ConvertMode=Report                                                     */
/* REVISION: 9.0          CREATED: 06/25/98     BY: *K1QZ* Suresh Nayak     */
/* REVISION: 9.0    LAST MODIFIED: 03/13/99     BY: *M0BD* Alfred Tan       */
/* REVISION: 9.1    LAST MODIFIED: 08/12/00     BY: *N0KP* myb              */
/* Revision: 1.25     BY: Anitha Gopal          DATE: 02/17/02  ECO: *N19G*  */
/* Revision: 1.26     BY: Santosh Rao           DATE: 04/19/02  ECO: *N1GQ*  */
/* $Revision: 1.27 $  BY: Mercy C.              DATE: 04/25/02  ECO: *N1H5*  */
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/

/* -----  W A R N I N G  -----  W A R N I N G  -----  W A R N I N G  -----  */
/*                                                                          */
/*         THIS PROGRAM IS A BOLT-ON TO STANDARD PRODUCT MFG/PRO.           */
/* ANY CHANGES TO THIS PROGRAM MUST BE PLACED ON A SEPARATE ECO THAN        */
/* STANDARD PRODUCT CHANGES.  FAILURE TO DO THIS CAUSES THE BOLT-ON CODE TO */
/* BE COMBINED WITH STANDARD PRODUCT AND RESULTS IN PATCH DELIVERY FAILURES.*/
/*                                                                          */
/* -----  W A R N I N G  -----  W A R N I N G  -----  W A R N I N G  -----  */

/* DISPLAY TITLE */

/*GUI preprocessor directive settings */
&SCOPED-DEFINE PP_GUI_CONVERT_MODE REPORT

{mfdeclre.i}

define input parameter pod_recno as recid no-undo.
define input parameter sch_recno as recid no-undo.
define input parameter report_yn as logical no-undo.
define input parameter use_calendardays as logical
   initial "yes" no-undo.
define input parameter asof_date as date no-undo.

define variable firm_upto as date no-undo.
define variable l_pod_sch_days as integer format ">>9" no-undo.
define variable ship_schedule as integer no-undo.
define variable new_cum_qty as decimal no-undo.
define variable l_delivery as logical no-undo.
define variable l_sdt_code like sdtd_code no-undo.
define variable idate as date no-undo.
define variable work_dt as date no-undo.
define variable last_firm_date as date no-undo.
define variable l_rlse_id like sch_rlse_id no-undo.
define variable curr_rel_id as integer no-undo.
define variable create_zero_sched like mfc_logical no-undo.
define variable zero_schedule like mfc_logical no-undo.
define variable zero_schedule_created like mfc_logical no-undo.

define new shared variable req_qty as decimal.
define new shared variable resch_ct as integer.
define new shared variable resch_qty as decimal extent 999 no-undo.
define shared variable date_based_rel_id like mfc_logical no-undo.

define buffer type_6_sch_mstr for sch_mstr.
define workfile work_schd like schd_det.

define shared temp-table t_schd_det no-undo like schd_det.

for first pod_det
   where recid(pod_det) = pod_recno
   exclusive-lock:
end. /* FOR FIRST pod_det */

for first sch_mstr
   where recid(sch_mstr) = sch_recno
   no-lock:
end. /* FOR FIRST sch_mstr */

for first scx_ref
   where scx_type  = 2
   and   scx_order = pod_nbr
   and   scx_line  = pod_line no-lock:
end. /* FOR FIRST scx_ref */

/* DELETING THE WORK FILE */
for each work_schd exclusive-lock:
   delete work_schd.
end. /* FOR EACH work_schd EXCLUSIVE-LOCK */

last_firm_date = sch_mstr.sch_pcs_date + pod_firm_days.

for last schd_det
   where schd_type    = sch_mstr.sch_type
   and   schd_nbr     = sch_mstr.sch_nbr
   and   schd_line    = sch_mstr.sch_line
   and   schd_rlse_id = sch_mstr.sch_rlse_id
   and   schd_fc_qual = "F" no-lock:
end. /* FOR LAST schd_det */

if available schd_det
then do:

   if pod_plan_days > 0
   then
      last_firm_date = max(last_firm_date,schd_date).

end. /* END OF IF AVAILABLE schd_det */

/* DETERMINE THE ENDING DATE FOR THE WEEKLY BUCKETS AND */
/* THE ENDING DATES FOR THE MONTHLY BUCKET.             */
assign
   l_pod_sch_days = pod_plan_days
   ship_schedule = 6.

/* GETTING NEXT RELEASE NUMBER FOR SHIPPING SCHEDULE */

if pod__qad10 = ? or pod__qad10 = ""
then
   pod__qad10 = string(1,"9999999").

if date_based_rel_id
then do:

   assign
      curr_rel_id = 0

      l_rlse_id = string(year(asof_date),"9999") +
                  string(month(asof_date),"99")  +
                  string(day(asof_date),"99")    + "-".

   for last type_6_sch_mstr
      where type_6_sch_mstr.sch_type = 6
      and   type_6_sch_mstr.sch_nbr  = pod_nbr
      and   type_6_sch_mstr.sch_line = pod_line
      and   type_6_sch_mstr.sch_rlse_id begins l_rlse_id
      exclusive-lock:
   end. /* FOR LAST type_6_sch_mstr */

   if not available type_6_sch_mstr
   then
      l_rlse_id = l_rlse_id + string(001,"999").
   else do:
      curr_rel_id =
         integer(substring(type_6_sch_mstr.sch_rlse_id,11,3)).

      if curr_rel_id + 1 > 999
      then
         l_rlse_id = l_rlse_id + string(001,"999").
      else
         l_rlse_id = l_rlse_id +
            string(integer(substring(type_6_sch_mstr.sch_rlse_id,11,3))
            + 1,"999").

      /* SET END EFFECTIVE DATE ON PREVIOUS SCHEDULE */
      type_6_sch_mstr.sch_eff_end = asof_date.

   end. /* ELSE DO: */
end. /* END OF IF date_based_rel_id */

else do:
   repeat while can-find(type_6_sch_mstr
      where type_6_sch_mstr.sch_type    = ship_schedule
      and   type_6_sch_mstr.sch_nbr     = pod_nbr
      and   type_6_sch_mstr.sch_line    = pod_line
      and   type_6_sch_mstr.sch_rlse_id = pod__qad10):

      pod__qad10 = string((integer(pod__qad10) + 1),"999999").
      if integer(pod__qad10) = 999999
      then
         pod__qad10 = string(1,"999999").

   end. /* END OF REPEAT WHILE CAN-FIND(sch_mstr) */
end. /* END OF ELSE DO */

create type_6_sch_mstr.

assign
   type_6_sch_mstr.sch_type       = ship_schedule
   type_6_sch_mstr.sch_nbr        = pod_nbr
   type_6_sch_mstr.sch_line       = pod_line
   type_6_sch_mstr.sch_rlse_id    = (if date_based_rel_id
                                     then
                                        l_rlse_id else pod__qad10)
   type_6_sch_mstr.sch_cr_date    = today
   type_6_sch_mstr.sch_cr_time    = time
   type_6_sch_mstr.sch_eff_start  = sch_mstr.sch_eff_start
   type_6_sch_mstr.sch_cumulative = sch_mstr.sch_cumulative
   type_6_sch_mstr.sch_fab_qty    = sch_mstr.sch_fab_qty
   type_6_sch_mstr.sch_fab_end    = sch_mstr.sch_fab_end
   type_6_sch_mstr.sch_fab_strt   = sch_mstr.sch_fab_strt
   type_6_sch_mstr.sch_pcr_qty    = sch_mstr.sch_pcr_qty
   type_6_sch_mstr.sch_pcs_date   = sch_mstr.sch_pcs_date
   type_6_sch_mstr.sch_raw_qty    = sch_mstr.sch_raw_qty
   type_6_sch_mstr.sch_raw_strt   = sch_mstr.sch_raw_strt
   type_6_sch_mstr.sch_raw_end    = sch_mstr.sch_raw_end
   type_6_sch_mstr.sch_sd_pat     = sch_mstr.sch_sd_pat
   type_6_sch_mstr.sch_sd_time    = sch_mstr.sch_sd_time.

if recid(type_6_sch_mstr) = -1
then .

   pod_curr_rlse_id[ship_schedule - 3] = type_6_sch_mstr.sch_rlse_id.

/* DETERMINE THE LAST DAY OF SCHEDULE */
if use_calendardays
then
   firm_upto = type_6_sch_mstr.sch_pcs_date + l_pod_sch_days.
else do:
   /* USING WORKING DAYS AS FIRM DAYS */
   {gprun1.i ""mfhdate.p""
      "(input sch_mstr.sch_pcs_date,
        input l_pod_sch_days,
        input scx_shipto,
        true,
        output firm_upto)"}

end. /* END OF ELSE DO */

assign
   l_sdt_code = ""
   l_delivery = no
   resch_ct   = 0
   resch_qty  = 0
   req_qty    = 0.

/* IF SDT CODE IN PO LINE ITEM DETAIL <> "" */
if pod__qad07 <> ""
then do:

   if can-find(first sdtd_det
      where sdtd_code = pod__qad07
      and   sdtd_time <> "")
   then
      assign
         l_delivery = yes
         l_sdt_code = pod__qad07.

   for each sdtd_det no-lock
      where sdtd_code = l_sdt_code:

      resch_ct = resch_ct + 1.
   end. /* FOR EACH sdtd_det NO-LOCK */

end. /* IF pod__qad07 <> "" */

/* PASS THROUGH TYPE 4 DETAIL RECORDS AND CREATE */
/* WORK FILE RECORDS                             */

for each schd_det no-lock
   where schd_type    = sch_mstr.sch_type
   and   schd_nbr     = sch_mstr.sch_nbr
   and   schd_line    = sch_mstr.sch_line
   and   schd_rlse_id = sch_mstr.sch_rlse_id
   and   schd_date    <= firm_upto:

   if not(l_delivery)
   then do:
      create work_schd.
      assign
         work_schd.schd_type      = type_6_sch_mstr.sch_type
         work_schd.schd_nbr       = type_6_sch_mstr.sch_nbr
         work_schd.schd_line      = type_6_sch_mstr.sch_line
         work_schd.schd_rlse_id   = type_6_sch_mstr.sch_rlse_id
         work_schd.schd_date      = schd_det.schd_date
         work_schd.schd_time      = schd_det.schd_time
         work_schd.schd_interval  = (if schd_det.schd_interval <> ""
                                     then
                                        schd_det.schd_interval
                                     else
                                        "D")
         work_schd.schd_reference = schd_det.schd_reference
         work_schd.schd_discr_qty = schd_det.schd_discr_qty
         work_schd.schd_fc_qual   = schd_det.schd_fc_qual .

   end. /* IF NOT(l_delivery_times)  */
   else do:

      req_qty = schd_det.schd_discr_qty.

      {gprun.i ""rcsdpnta.p""}

      resch_ct = 0.
      for each sdtd_det no-lock
         where sdtd_code = l_sdt_code:

         resch_ct = resch_ct + 1.

         create work_schd.
         assign
            work_schd.schd_type      = type_6_sch_mstr.sch_type
            work_schd.schd_nbr       = type_6_sch_mstr.sch_nbr
            work_schd.schd_line      = type_6_sch_mstr.sch_line
            work_schd.schd_rlse_id   = type_6_sch_mstr.sch_rlse_id
            work_schd.schd_discr_qty = resch_qty[resch_ct]
            work_schd.schd_date      = schd_det.schd_date
            work_schd.schd_time      = sdtd_time
            work_schd.schd_interval  = "D"
            work_schd.schd_reference = schd_det.schd_reference
            work_schd.schd_fc_qual   = schd_det.schd_fc_qual .

      end. /* END OF FOR EACH sdtd_det */

   end. /* END OF ELSE DO */

end. /* END OF FOR EACH schd_det */
run find_prev_plan_sched (input recid(sch_mstr),
                          input recid(pod_det),
                          output zero_schedule,
                          output zero_schedule_created).

/* WE ARE GOING TO ONLY MAKE A ONE LINE ZERO SCHEDULE IF
ZERO SCHEDULES ARE TO BE GENERATED. OTHERWISE WE WILL
NOT PRINT A SCHEDULE IF THERE ARE NO REQUIREMENTS */

for first rsc_ctrl no-lock:
end. /* FOR FIRST rsc_ctrl NO-LOCK */

if not available rsc_ctrl
then
   create rsc_ctrl.

for first po_mstr
   where po_nbr = pod_nbr no-lock:
end. /* FOR FIRST po_mstr */

for first vd_mstr
   where vd_addr = po_vend no-lock:
end. /* FOR FIRST vd_mstr */

for first qad_wkfl
   where qad_key1 = "vd_mstr_ash"
   and qad_key2 = vd_addr no-lock:
end. /* FOR FIRST qad_wkfl */

if available qad_wkfl
then do:
   if substring(qad_charfld[4],1,3) = "yes"
   then
      create_zero_sched = yes.
   else
      create_zero_sched = no.
end. /* THEN DO */
else
   create_zero_sched = rsc__qadl01.

if firm_upto <> ?
then do:
   idate = type_6_sch_mstr.sch_pcs_date + 1.

   do while idate <= firm_upto:
      work_dt = ?.

      if idate <= firm_upto
      then
         assign
            work_dt = idate
            idate   = idate + 1.

      if work_dt <> ?
      then do:

         if not(l_delivery)
         then do:
            create work_schd.

            assign
               work_schd.schd_date     = work_dt
               work_schd.schd_interval = "D" .

         end. /* END OF IF NOT(l_delivery) */
         else do:
            for each sdtd_det no-lock
               where sdtd_code = l_sdt_code:

               create work_schd.
               assign
                  work_schd.schd_date     = work_dt
                  work_schd.schd_interval = "D"
                  work_schd.schd_time     = sdtd_time .

            end. /* END OF FOR EACH sdtd_det */

         end. /* END OF ELSE DO */
      end. /* END OF IF work_dt <> ? */
   end. /* END OF DO WHILE idate <= firm_upto */
end. /* END OF IF firm_upto <> ? */

/* CONSOLIDATE THE WORK FILE RECORDS AND CREATE SCHEDULE */
/* DETAIL RECORDS                                        */

for each work_schd no-lock
   break by work_schd.schd_date
         by work_schd.schd_time
         by work_schd.schd_reference:

   if create_zero_sched
   and zero_schedule
   and zero_schedule_created
   then
      next.

   if zero_schedule
   and not create_zero_sched
   and not zero_schedule_created
   then
      next.

   accumulate
      work_schd.schd_discr_qty
      (sub-total by work_schd.schd_reference).

   if last-of(work_schd.schd_reference)
   then do:

      create schd_det.

      assign
         schd_det.schd_type      = type_6_sch_mstr.sch_type
         schd_det.schd_nbr       = type_6_sch_mstr.sch_nbr
         schd_det.schd_line      = type_6_sch_mstr.sch_line
         schd_det.schd_rlse_id   = type_6_sch_mstr.sch_rlse_id
         schd_det.schd_date      = work_schd.schd_date
         schd_det.schd_time      = work_schd.schd_time
         schd_det.schd_interval  = work_schd.schd_interval
         schd_det.schd_reference = work_schd.schd_reference
         schd_det.schd_discr_qty = accum sub-total by
         work_schd.schd_reference work_schd.schd_discr_qty .

      if recid(schd_det) = -1
      then .

      if schd_det.schd_date <= last_firm_date
      then
         schd_det.schd_fc_qual = "F".
      else
         schd_det.schd_fc_qual = "P" .

      if zero_schedule
      then
         zero_schedule_created = yes.

      create t_schd_det.
      buffer-copy schd_det to t_schd_det.

   end. /* END OF IF last-of(schd_reference)  */

end. /* FOR EACH work_schd */

if resch_ct >=2
then do:

   {gprun.i ""rcordmlt.p""
      "(input recid(type_6_sch_mstr),
        input pod_ord_mult,
        input report_yn)"}

end.  /* END OF IF resch_ct >=2 */

/* UPDATE CUM QUANTITIES */

new_cum_qty = type_6_sch_mstr.sch_pcr_qty.

for each schd_det exclusive-lock
   where schd_det.schd_type    = type_6_sch_mstr.sch_type
   and   schd_det.schd_nbr     = type_6_sch_mstr.sch_nbr
   and   schd_det.schd_line    = type_6_sch_mstr.sch_line
   and   schd_det.schd_rlse_id = type_6_sch_mstr.sch_rlse_id:

   assign
      new_cum_qty           = new_cum_qty + schd_det.schd_discr_qty
      schd_det.schd_cum_qty = new_cum_qty.

end. /* FOR EACH schd_det */

/* REPORT THE SHIPPING SCHEDULE */
if report_yn
then do:

   {gprun.i
      ""rsiq1a.p""
      "(input type_6_sch_mstr.sch_type,
        input type_6_sch_mstr.sch_nbr,
        input type_6_sch_mstr.sch_line,
        input type_6_sch_mstr.sch_rlse_id,
        input no,
        input no)"
      }

end. /* END OF IF report_yn */

PROCEDURE find_prev_plan_sched:

   define input parameter sch_recno as recid no-undo.
   define input parameter pod_recno as recid no-undo.
   define output parameter zero_schedule like mfc_logical no-undo.
   define output parameter zero_schedule_created like mfc_logical
      no-undo.

   assign
      zero_schedule = no
      zero_schedule_created = no.

   for first sch_mstr
      where recid(sch_mstr) = sch_recno
   no-lock:
   end. /* FOR FIRST sch_mstr */

   for first pod_det
      where recid(pod_det) = pod_recno
   no-lock:
   end. /* FOR FIRST pod_det */

   for first schd_det
      where schd_type    = sch_type
      and   schd_nbr     = sch_nbr
      and   schd_line    = sch_line
      and   schd_rlse_id = sch_rlse_id
   no-lock:
   end. /* FOR FIRST schd_det */

   if available schd_det
   then do:
      find first schd_det
         where schd_type      = sch_type
         and   schd_nbr       = sch_nbr
         and   schd_line      = sch_line
         and   schd_rlse_id   = sch_rlse_id
         and   schd_discr_qty > 0 no-lock no-error.
      if available schd_det
      then
         zero_schedule = no.
   end. /* IF AVAILABLE schd_det */
   else
      zero_schedule = yes.

   find prev sch_mstr
      where sch_type = 4
      and   sch_nbr  =  pod_nbr
      and   sch_line = pod_line
      use-index sch_cr_date no-lock no-error.

   if available sch_mstr
   then do:
      find first schd_det
         where schd_type    = 6
         and   schd_nbr     = sch_nbr
         and   schd_line    = sch_line
         and   schd_rlse_id = sch_rlse_id
         no-lock no-error.

      if not available schd_det
      then
         zero_schedule_created = no.
      else do:
         find first schd_det
            where schd_type      = 6
            and   schd_nbr       = sch_mstr.sch_nbr
            and   schd_line      = sch_mstr.sch_line
            and   schd_rlse_id   = sch_mstr.sch_rlse_id
            and   schd_discr_qty > 0
         no-lock no-error.
         if available schd_det
         then
            zero_schedule_created = no.
         else
            zero_schedule_created = yes.
      end. /* ELSE DO */
   end. /* IF AVAILABLE sch_mstr */
   else
      zero_schedule_created = no.
END PROCEDURE. /* PROCEDURE */
